// https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/

// 算法思路总结：
// 1. 使用贪心策略求解多次股票买卖最大利润
// 2. 寻找所有上升区间的价格差并累加
// 3. 在每个上升区间低点买入，高点卖出
// 4. 时间复杂度：O(n)，空间复杂度：O(1)

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

class Solution 
{
public:
    int maxProfit(vector<int>& prices) 
    {
        int m = prices.size();
        int ret = 0;

        for (int i = 0 ; i < m ; )
        {
            int j = i;
            while (j + 1 < m && prices[j] <= prices[j + 1])
                j++;
            
            ret += prices[j] - prices[i];
            i = j + 1;
        }

        return ret;
    }
};

int main()
{
    vector<int> v1 = {7,1,5,3,6,4}, v2 = {1,2,3,4,5};
    Solution sol;

    cout << sol.maxProfit(v1) << endl;
    cout << sol.maxProfit(v2) << endl;

    return 0;
}